//
//  AWMsgCenteManage.m
//  JFBase
//
//  Created by Alan Wang on 2018/8/20.
//  Copyright © 2018年 Alan Wang. All rights reserved.
//

#import "JFMsgCenteManager.h"
#import "JFStringHelper.h"
#import "JFAppCrashProcessManager.h"

@interface JFMsgCenteManager()
@property(nonatomic,strong) NSMutableDictionary * moduleMap;
@end

@implementation JFMsgCenteManager
+ (instancetype)sharedInstance {
    static JFMsgCenteManager *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[JFMsgCenteManager alloc] init];
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
            [JFAppCrashProcessManager progressWKContentViewCrash];
        }
    });
    return _sharedClient;
}
-(instancetype)init{
    self = [super init];
    if (self) {
        _moduleMap = [[NSMutableDictionary alloc] init];
        _debug = NO;
    }
    return self;
}
-(JFResultVo*)registerModule:(id<JFMsgCenteProtocol>)instance moduleName:(NSString*)moduleName{
    JFResultVo * result = [[JFResultVo  alloc] init];
    
    if([self checkModuleCanRegster:moduleName]){
        if(!instance){
            result.errorNo = -2;
            result.errorInfo  = @"注册模块失败,模块实例不能空";
        }else{
            _moduleMap[moduleName] = instance;
            result.errorNo = 0;
            result.errorInfo  = @"注册成功";
        }
    }else{
        result.errorNo = -1;
        result.errorInfo  = @"注册模块失败,模块名为空或已被注册";
    }
    return result;
}

-(BOOL)checkModuleCanRegster:(NSString*)moduleName{
    if(moduleName&&moduleName.length){
        id module = _moduleMap[moduleName];
        if(module){
            return NO;
        }else{
            return YES;
        }
    }else{
        return NO;
    }
}
-(JFMsgCenterVo*)requestWithMsgCenterVo:(JFMsgCenterVo*)requestVo callback:(JFCallBack)block{
   
    JFMsgCenterVo * vo = nil;
    JFResultVo * result = [[JFResultVo alloc] init];
    if([JFStringHelper isEmpty:requestVo.from] && [JFStringHelper isEmpty:requestVo.to]){//插件间调用

        NSString * funcNum = requestVo.requestNum;
        if([JFStringHelper isNotEmpty:funcNum]){
            NSString *  actionName=[NSString stringWithFormat:@"AWFounction%@",funcNum];
            Class class = NSClassFromString(actionName);
            //如果存在实例插件
            if (class)
            {
                id<JFMsgCenteProtocol> instance = [[class alloc] init];
                AWMsgCenterCallBack callback = requestVo.callback;
                if(!callback){
                    if([JFMsgCenteManager sharedInstance].isDebug){
                        NSString * msg = [NSString stringWithFormat:@"MsgCenter:插件调用:No:%@-Msg:%@",requestVo.requestNum,@"调用插件同步方法"];
                        NSLog(@"%@",msg);
                    }
                    vo = [instance syncFunctionForward:requestVo];
                }else{
                    if([JFMsgCenteManager sharedInstance].isDebug){
                        NSString * msg = [NSString stringWithFormat:@"MsgCenter:插件调用:No:%@-Msg:%@",requestVo.requestNum,@"调用插件异步方法"];
                        NSLog(@"%@",msg);
                    }
                    [instance asyncFunctionForward:requestVo];
                }
                return vo;
            }else{
                result.errorNo = -1;
                result.errorInfo = @"无法找到可执行插件";
                NSString * msg = [NSString stringWithFormat:@"MsgCenter:插件调用:No:%@-Msg:%@",requestVo.requestNum,@"无法找到可执行插件"];
                [self debugLog:msg];
            }
            
            
        }else{
            result.errorNo = -1;
            result.errorInfo = @"无法找到可执行插件";
            NSString * msg = [NSString stringWithFormat:@"MsgCenter:插件调用:No:%@-Msg:%@",requestVo.requestNum,@"无法找到可执行插件"];
            [self debugLog:msg];
        }

    }else{//模块间调用
        if([JFStringHelper isNotEmpty:requestVo.from]){
            
            NSString * to = requestVo.to;
            if([JFStringHelper isEmpty:to]){
                if([JFMsgCenteManager sharedInstance].isDebug){
                    NSString * msg = [NSString stringWithFormat:@"MsgCenter:模块间调用:No:%@-From:%@-To:%@-Msg:%@",requestVo.requestNum,requestVo.from,requestVo.to,@"广播插件"];
                    NSLog(@"%@",msg);
                }
                for (NSString * key in _moduleMap.allKeys) {
                    id<JFMsgCenteProtocol> instance = _moduleMap[key];
                    [instance asyncFunctionForward:requestVo];
                }
            }else{
                
                NSString * respodNum = requestVo.respodNum;
                AWMsgCenterCallBack callback = requestVo.callback;
                id<JFMsgCenteProtocol> instance = _moduleMap[to];
                if(!instance){
                    result.errorNo = -1;
                    NSString * errorInfo = [NSString stringWithFormat:@"模块%@未注册",to];
                    result.errorInfo = errorInfo;
                    NSString * msg = [NSString stringWithFormat:@"MsgCenter:模块间调用:No:%@-From:%@-To:%@-Msg:%@",requestVo.requestNum,requestVo.from,requestVo.to,errorInfo];
                    [self debugLog:msg];
                }
                
                if(!callback && [JFStringHelper isEmpty:respodNum]){
                    if([JFMsgCenteManager sharedInstance].isDebug){
                        NSString * msg = [NSString stringWithFormat:@"MsgCenter:模块间调用:No:%@-From:%@-To:%@-Msg:%@",requestVo.requestNum,requestVo.from,requestVo.to,@"同步调用"];
                        NSLog(@"%@",msg);
                    }
                    vo = [instance syncFunctionForward:requestVo];
                    vo.from = requestVo.to;
                    vo.to = requestVo.from;
                }else{
                    if([JFMsgCenteManager sharedInstance].isDebug){
                        NSString * msg = [NSString stringWithFormat:@"MsgCenter:模块间调用:No:%@-From:%@-To:%@-Msg:%@",requestVo.requestNum,requestVo.from,requestVo.to,@"异步调用"];
                        NSLog(@"%@",msg);
                    }
                    [instance asyncFunctionForward:requestVo];
                }
            }
        }else{
            NSString * msg = [NSString stringWithFormat:@"MsgCenter:模块间调用:No:%@-From:%@-To:%@-Msg:%@",requestVo.requestNum,requestVo.from,requestVo.to,@"模块间调用from不能为空"];
            result.errorNo = -1;
            result.errorInfo = msg;
            [self debugLog:msg];
        }
    }
    
    if(block){
        block(result);
    }
    return vo;
}

-(void)debugLog:(NSString*)str{
    if ([JFMsgCenteManager sharedInstance].isDebug) {
        NSLog(@"%@",str);
    }
}

+(JFResultVo*)registerModule:(id<JFMsgCenteProtocol>)instance moduleName:(NSString*)moduleName{
    return [[JFMsgCenteManager sharedInstance] registerModule:instance moduleName:moduleName];
}
//返回值 只对 返回同步调用有意义
+(JFMsgCenterVo*)requestWithMsgCenterVo:(JFMsgCenterVo*)requestVo callback:(JFCallBack)block{
    return [[JFMsgCenteManager sharedInstance] requestWithMsgCenterVo:requestVo callback:block];
}
@end
